package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderSearchList 
{
    private JPanel 
            orderSearchPanel,
            scrollPanel;
    private OrderContainer orderContainer;
    private JScrollPane scrollPane;

    public OrderSearchList(OrderContainer view)
    {
        orderContainer = view;
        
        orderSearchPanel = new JPanel(null);
        orderSearchPanel.setBounds(320, 0, 580, 500);
        orderSearchPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Åbne Ordrer", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        orderSearchPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        scrollPane = new JScrollPane();
        buildExistingScrollPanel("WHERE finished = 0", "ORDER BY orderID DESC", "");
        
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 540, 450);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        orderSearchPanel.add(scrollPane);
        
        orderSearchPanel.setVisible(true);
    }
    
    public void buildExistingScrollPanel(String where, String orderby, String limit)
    {
        scrollPanel.removeAll();
        scrollPane.getViewport().setViewPosition(new Point(0,0));
        try
        {
            model.Order[] orders = model.handlers.OrderHandler.getAllOrders(where, orderby, limit);
            for (int i = 0; i < orders.length; i++)
            {
                OrderSearchListPanel panel = new OrderSearchListPanel(orderContainer, orders[i]);
                panel.setBounds(10, 10 + (i * 175), 500, 165);
                scrollPanel.setPreferredSize(new Dimension(500, 200 + (i * 165)));
                scrollPanel.add(panel);
            }
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }

    public JPanel getPanel()
    {
        return orderSearchPanel;
    }
}